package cn.adelyn.blog.blogmanager.test;

import cn.adelyn.base.api.auth.pojo.bo.UserInfoInTokenBO;
import cn.adelyn.base.api.snowfalke.feign.SnowflakeFeignClient;
import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.blogmanager.BlogManagerApplication;
import cn.adelyn.blog.blogmanager.constant.BlogConstant;
import cn.adelyn.blog.blogmanager.controller.BlogController;
import cn.adelyn.blog.blogmanager.dao.BlogDAOService;
import cn.adelyn.blog.blogmanager.pojo.bo.InsertBlogDecryptBO;
import cn.adelyn.blog.blogmanager.pojo.dto.InsertBlogDTO;
import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = BlogManagerApplication.class)
@Slf4j
public class BlogManagerTester {

    @Autowired
    BlogController blogController;

    @Autowired
    SnowflakeFeignClient snowflakeFeignClient;

    @Autowired
    BlogDAOService blogDAOService;


    @Test
    void insertBlog() {
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUserId(2L);
        userInfoInTokenBO.setUserType(1);
        AuthUserContext.set(userInfoInTokenBO);

        List<String> fileIdList = new ArrayList<>();
        fileIdList.add("21333123L");
        fileIdList.add("43232L");

        // 加密的
        List<String> picIdList = new ArrayList<>();
        picIdList.add("34232L");
        picIdList.add("3423121L");
        picIdList.add("53415L");

        InsertBlogDTO insertBlogDTO = new InsertBlogDTO();
        insertBlogDTO.setBlogId(snowflakeFeignClient.snowflakeId().getData());
        insertBlogDTO.setBlogContent("this is content");
        insertBlogDTO.setBlogTitle("this is title");
        insertBlogDTO.setBlogVisible(BlogConstant.VISIBLE_PUBLIC);
        insertBlogDTO.setPicIdList(picIdList);
        insertBlogDTO.setFileIdList(fileIdList);

        log.info("insertBlogDTO: {}", insertBlogDTO);

        blogController.insertBlog(insertBlogDTO);
    }

    @Test
    void updateBlog() {
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUserId(2L);
        userInfoInTokenBO.setUserType(1);
        AuthUserContext.set(userInfoInTokenBO);

        // 加密的
        List<String> fileIdList = new ArrayList<>();
        fileIdList.add("21333123L");
        fileIdList.add("43232L");

        InsertBlogDTO insertBlogDTO = new InsertBlogDTO();
        insertBlogDTO.setBlogId(snowflakeFeignClient.snowflakeId().getData());
        insertBlogDTO.setBlogContent("this is content");
        insertBlogDTO.setBlogTitle("this is title");
        insertBlogDTO.setBlogVisible(BlogConstant.VISIBLE_PUBLIC);
        insertBlogDTO.setFileIdList(fileIdList);

        blogController.insertBlog(insertBlogDTO);

        // null 值的字段不会被 update
        insertBlogDTO.setAuthorId(0L);
        insertBlogDTO.setBlogTitle("title is updated");

        blogController.updateBlog(insertBlogDTO);
    }

    @Test
    void cocurrentInsertBlog() {

        ConcurrencyTester tester = ThreadUtil.concurrencyTest(20, () -> {

            InsertBlogDTO insertBlogDTO = new InsertBlogDTO();
            Long blogId = snowflakeFeignClient.snowflakeId().getData();
            insertBlogDTO.setBlogId(blogId);
            insertBlogDTO.setBlogContent("this is content");
            insertBlogDTO.setBlogTitle("this is title");

            blogController.insertBlog(insertBlogDTO);

            log.info("{} test finished, blogId: {}", Thread.currentThread().getName(), blogId);
        });

        log.info(String.valueOf(tester.getInterval()));
    }

    @Test
    void insertBlogBatch() {
        List<Long> fileIdList = new ArrayList<>();
        fileIdList.add(21333123L);
        fileIdList.add(43232L);

        List<Long> picIdList = new ArrayList<>();
        picIdList.add(34232L);
        picIdList.add(3423121L);
        picIdList.add(53415L);

        InsertBlogDecryptBO insertBlogDecryptBO = new InsertBlogDecryptBO();
        insertBlogDecryptBO.setBlogId(snowflakeFeignClient.snowflakeId().getData());
        insertBlogDecryptBO.setBlogContent("this is content");
        insertBlogDecryptBO.setBlogTitle("this is title");
        insertBlogDecryptBO.setFileIdList(fileIdList);
        insertBlogDecryptBO.setPicIdList(picIdList);

        blogDAOService.insertBlog(insertBlogDecryptBO);
    }
}
